********************************************************************************
* This dofile analysis the effects on educational spending and enrolment of the DWs family members.
* The analysis for spending is done at the household level while enrolment is done at the individual level.
* Table 5 is exported at the end of the dofile.
********************************************************************************

use "$data_prep/owwa_data_merged_long", clear
estimates clear

********************************************************************************
* Gen variable for household size
egen household_size_ =rowmax(h_hhid_mem*_1)
gen educ_year_1 = hh_hhe24_educ_year_1
gen educ_year_3 = hh_c23_educ_year_3


* Impute missings for baseline educational spending with zeros in wave 1
gen mis_educ_year_1	= educ_year_1>=.
replace	educ_year_1 = 0 if mis_educ_year_1==1

* Create log variables
foreach var in educ_year_1 educ_year_3 {
	gen l_`var' = log(`var' +1)
} 
 
* Get 99th percentile 
sum educ_year_3, d
local educ_year_3_99=r(p99)

estimates clear

********************************************************************************
* Effects on educational spending (household level)
* Loop over the five samples: all, HK, KSA, talk, notalk

forvalues i = 4(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"
	else if `i'==4 {
		local sample = "notalk"
		local j=1
	}
	else if `i'==3 {
		local sample = "talk"
		local j=0
	}	
	display "`sample'"
	
	preserve
	
	if `i'<3{
		drop if ksa==`i'
		tab ksa
	}
	else if `i'>=3{
		drop if talk_employer==`j'
	}

	* Drop DWs that never went to HK/KSA
	* NOTE: In the latest version of the paper, this selection was not done for household level analysis (slightly changes results of columns 1 and 2 in table 5)
	keep if status_3 < 5 | status_3 >= .

	* Log of Several Expenditures
	regout l_educ_year_3 l_educ_year_1 mis_educ_year_1 $controls i.wave if gift<. & wave==3, cluster(cluster) treat(gift) estname(l_educ_year_`sample')
	
	* Several Expenditures (level winsorized at 99 percentile) 
	regout educ_year_3 educ_year_1 mis_educ_year_1 $controls i.wave if gift<. & wave==3 & educ_year_3 < `educ_year_3_99', cluster(cluster) treat(gift) estname(educ_year_`sample')

	restore
}

 global controls "finlit sharing account child max_highschool college age nointernet ksa baseline_mis direct_3 lduration_"

* Rename variables for reshape
	count 
	local obs = r(N)
	
forvalues i=1/14 {

	rename hh_rc1_enrolled_`i'_1 enrolled_1_`i'
	rename hh_l27_enrolled_`i'_3 enrolled_3_`i'

	rename hh_rc2_school_`i'_1 private_school_1_`i'
	rename hh_l28_school_`i'_3 private_school_3_`i'
	
	capture rename hh_r7_have_job_`i'_1 have_job_1_`i' 
	capture rename hh_r15_status_`i'_1 econ_status_1_`i'

	capture rename hh_l16_have_job_`i'_3 have_job_3_`i'
	capture rename hh_l19_type_worker_`i'_3 type_worker_3_`i'	
	
	rename h_q2_hhage`i'_1 age_1_`i'
	rename hh_allmembersloop_`i'_age_3 age_3_`i'

	rename h_q5_hhsex`i'_1 male_`i'
	
	rename h_q1a_rela`i'_1 rela_hsw_1_`i'
	rename hh_r2b_rela`i'_1 hh_rela_hsw_1_`i'
	
}
**** Reshape to use hh member data *********************************************

keep talk_employer have_job_1_* have_job_3_* econ_status_1_* type_worker_3_* status_3 ///
	enrolled_1_* enrolled_3_* male_* private_school_1_* private_school_3_* ///
	age_1_* age_3_* rela_hsw_1_* hh_rela_hsw_1_* $controls duration_3 ///
	gift hh_choosechildren_c1_3 hh_choosechildren_c2_3 hh_choosechildren_c1_1 hh_choosechildren_c2_1 ///
	rno wave cluster direct_3 _mi_miss
	
	* Generate single identifier
	tostring rno, gen(rno_str)
	tostring wave, gen(wave_str)
	gen rno_wave = rno_str + "_" + wave_str
	drop rno_str wave_str
	
	mi reshape long have_job_1_ have_job_3_ econ_status_1_ type_worker_3_  ///
	enrolled_1_ enrolled_3_ male_ private_school_1_ private_school_3_ ///
	age_1_ age_3_ rela_hsw_1_ hh_rela_hsw_1_, i(rno_wave) j(hh_member)  

	rename hh_choosechildren_c1_1 child_1_1
	rename hh_choosechildren_c2_1 child_2_1
	rename hh_choosechildren_c1_3 child_1_3
	rename hh_choosechildren_c2_3 child_2_3

	* Measure of hh_size and by age group
	foreach w in 1 3 {
		gen age_3_12_`w' = 1 if age_`w'_>=3 & age_`w'_<=12
		gen age_13_25_`w' = 1 if age_`w'_>=13 & age_`w'_<=25

		gen age_3_14_`w' = 1 if age_`w'_>=3 & age_`w'_<=14
		gen age_15_25_`w' = 1 if age_`w'_>=15 & age_`w'_<=25
		gen age_26older_`w' = 1 if age_`w'_ >= 26 & age_`w'_<=.
		gen age_26_40_`w' = 1 if age_`w'_ >= 26 & age_`w'_<=40
		gen age_41_60_`w' = 1 if age_`w'_ >= 41 & age_`w'_<=60
		gen age_61older_`w' = 1 if age_`w'_ >= 61 & age_`w'_<=.
		
		recode age_3_12_`w' age_13_25_`w' age_3_14_`w' age_15_25_`w' age_26older_`w' age_26_40_`w' age_41_60_`w' age_61older_`w' (.=0) if age_`w'_ <.

	}
	
	bysort rno_wave: gen pid = _n		
		
	* Create one variable that holds the relation to the HSW
	* Note: This information is available from the HSW baseline and the household baseline
	replace rela_hsw_1_ = hh_rela_hsw_1_ if rela_hsw_1_>=. & hh_rela_hsw_1_<=.
	
	gen mis_enrolled_1 = enrolled_1_>=.
	replace enrolled_1_ = 0 if mis_enrolled_1

	gen mis_have_job_1 = have_job_1_>=.
	replace have_job_1_ = 0 if mis_have_job_1
	
	gen mis_private_school_1_ = private_school_1_ >= .
	replace private_school_1_ = 0 if mis_private_school_1_


*** Loop over the five samples: all, HK, KSA, talk, notalk	
forvalues i = 4(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"
	else if `i'==4 {
		local sample = "notalk"
		local j=1
	}
	else if `i'==3 {
		local sample = "talk"
		local j=0
	}	
	display "`sample'"
	
	preserve
	
	if `i'<3{
		drop if ksa==`i'
		tab ksa
	}
	else if `i'>=3{
		drop if talk_employer==`j'
	}
	
	* Drop DWs that never went to HK/KSA
	keep if status_3 < 5 | status_3 >= .

	* Educational enrollment
	foreach var in enrolled  {
		regout `var'_3_ i.age_3_ male_ enrolled_1_ mis_enrolled_1  $controls if wave==3 & age_3_>=6 & age_3_<=20, ///
			cluster(cluster) treat(gift) estname(`var'_`sample')	
		regout `var'_3_ i.age_3_ male_ enrolled_1_ mis_enrolled_1 $controls if wave==3 & rela_hsw_1>=2 & rela_hsw<=4, ///
			cluster(cluster) treat(gift) estname(`var'_child_`sample') // We select rela_hsw=2 (biological child) =3 (stepson/daughter) = 4 (father/mother as some enumerators seem to have reversed the coding of the family relationship)
		regout `var'_3_ i.age_3_ male_ enrolled_1_ mis_enrolled_1 $controls if wave==3 & age_3_<=12 & age_3_>=6, ///
			cluster(cluster) treat(gift) estname(`var'_young_`sample')
		regout `var'_3_ i.age_3_ male_ enrolled_1_ mis_enrolled_1 $controls if wave==3 & age_3_>=13 & age_3_<=20, ///
			cluster(cluster) treat(gift) estname(`var'_old_`sample')
	}
	
	* Has a job
	foreach var in have_job {
		regout `var'_3_ i.age_3_ male_ have_job_1_ mis_have_job_1 $controls if wave==3 & age_3_<=25, ///
			cluster(cluster) treat(gift) estname(`var'_`sample')	
	}
	
	restore
}

	* Export Table 5
	esttab educ_year_All  l_educ_year_All enrolled_All enrolled_young_All enrolled_old_All have_job_All  ///
	using "$outpath_tab/table5.tex", ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment ///
	mtitles("USD" "Log (+1)" "All" "Age 3-12" "Age 13-20" "Age 16-25"  ) ///
	mgroups("Education expenditure" "Enrolled" "Employed", pattern(1 0 1 0 0 1 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	posthead("\midrule" "&\multicolumn{6}{c}{\textit{Full}}\\") 
		 
	esttab educ_year_HK  l_educ_year_HK enrolled_HK enrolled_young_HK enrolled_old_HK have_job_HK ///
	using "$outpath_tab/table5.tex",nonumbers ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{6}{c}{\textit{Hong Kong}}\\") 
	
	esttab educ_year_KSA  l_educ_year_KSA enrolled_KSA enrolled_young_KSA enrolled_old_KSA have_job_KSA  ///
	using "$outpath_tab/table5.tex",nonumbers ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{6}{c}{\textit{Saudi Arabia}}\\") 	

	esttab educ_year_notalk  l_educ_year_notalk enrolled_notalk enrolled_young_notalk enrolled_old_notalk have_job_notalk ///
	using "$outpath_tab/table5.tex",nonumbers ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{6}{c}{\textit{Did not talk to employer}}\\") 
	
	esttab educ_year_talk l_educ_year_talk enrolled_talk enrolled_young_talk enrolled_old_talk have_job_talk  ///
	using "$outpath_tab/table5.tex",nonumbers ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{6}{c}{\textit{Talked to employer}}\\") 	
			